﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DIS_Web_Client.DIS_WS_BusinessLayer;

namespace DIS_WEB_Client.Helpers
{
    public class CommonHelpers
    {
        private static int RetrieveOnlineWarehouse()
        {
            // Select Online Warehouse
            var onlineWareHouse = BusService.Instance.ChemistWareHouse_SelectAll().First(n => n.Name.ToUpper().Contains("ONLINE"));
            if (onlineWareHouse != null)
            {
                return onlineWareHouse.ID;
            } 
            return -1;
        }

        private static double GetPromotionalItem(ItemDTO item)
        {
            // Priority of local is higher than national
            var localItem = BusService.Instance.LocalPromotion_SelectByIDItemAndIDChemistWareHouse(item.ID, RetrieveOnlineWarehouse());
            var nationalpromotion = BusService.Instance.NationalPromotion_SelectByIDItem(item.ID);
            if (localItem != null)
            {
                // Checking if national promotion is exist and it's value is less than local promotion
                if (nationalpromotion != null && nationalpromotion.Discount >= localItem.Discount)
                {
                    return nationalpromotion.Discount;
                }
                return localItem.Discount;
            }
            else
            {
                var nationalItem = BusService.Instance.NationalPromotion_SelectByIDItem(item.ID);
                if (nationalItem != null)
                {
                    return nationalItem.Discount;
                }
                return 0;
            }
        }

        public static double CalcPromotionPrice(ItemDTO item)
        {
            var originalPrice = item.Price;
            var promotion = GetPromotionalItem(item);
            if (promotion != 0)
            {
                return originalPrice - ((originalPrice * promotion) / 100);
            }
            return originalPrice;
        }
    }
}